import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import store from "../store"
//引入一级路由
const Login = () => import("../pages/Login/Login.vue")
const Index = () => import("../pages/Index/Index.vue")


// 引入二级路由
// const AddMenu = () => import("../views/Menu/components/AddMenu.vue")
const Home = () => import("../views/Home/Home.vue")
const Menu = () => import("../views/Menu/Menu.vue")
const Role = () => import("../views/Role/Role.vue")
const Admin = () => import("../views/Admin/Admin.vue")
const CateGoods = () => import("../views/CateGoods/CateGoods.vue")
const GoodsSpec = () => import("../views/GoodsSpec/GoodsSpec.vue")
const goodsManager = () => import("../views/goodsManager/goodsManager.vue")
const Vip = () => import("../views/Vip/Vip.vue")
const Banners = () => import("../views/Banners/Banners.vue")
const Seckill = () => import("../views/Seckill/Seckill.vue")

function fn(url) {
  return store.state.user.menus_url.some(item => item === url);
}

const routes = [
  {
    path: "/index",
    component: Index,
    meta: { title: "首页" },
    children: [
      {
        path: "home",
        component: Home,
        meta: { title: "Echarts" },
        name: "首页"
      },
      {
        path: "menu",
        component: Menu,
        meta: { title: "菜单信息" },
        name: "菜单管理",
        beforeEnter(to, from, next) {
          fn("/index/menu") ? next() : next("/index");
        }
      },
      {
        path: "role",
        component: Role,
        meta: { title: "角色信息" },
        name: "角色管理",
        beforeEnter(to, from, next) {
          fn("/index/role") ? next() : next("/index");
        }
      },
      {
        path: "admin",
        component: Admin,
        meta: { title: "管理员信息" },
        name: "管理员管理",
        beforeEnter(to, from, next) {
          fn("/index/admin") ? next() : next("/index");
        }
      }, 
      {
        path: "categoods",
        component: CateGoods,
        meta: { title: "商品分类信息" },
        name: "商品分类",
        beforeEnter(to, from, next) {
          fn("/index/categoods") ? next() : next("/index");
        }
      },
      {
        path: "goodsSpec",
        component: GoodsSpec,
        meta: { title: "商品规格信息" },
        name: "商品规格",
        beforeEnter(to, from, next) {
          fn("/index/goodsSpec") ? next() : next("/index");
        }
      },
      {
        path: "goodsManager",
        component: goodsManager,
        meta: { title: "商品管理信息" },
        name: "商品管理",
        beforeEnter(to, from, next) {
          fn("/index/goodsManager") ? next() : next("/index");
        }
      },
      {
        path: "vip",
        component: Vip,
        meta: { title: "会员信息" },
        name: "会员管理",
        beforeEnter(to, from, next) {
          fn("/index/vip") ? next() : next("/index");
        }
      },
      {
        path: "banners",
        component: Banners,
        meta: { title: "轮播图信息" },
        name: "轮播图管理",
        beforeEnter(to, from, next) {
          fn("/index/banners") ? next() : next("/index");
        }
      },
      {
        path: "seckill",
        component: Seckill,
        meta: { title: "秒杀活动" },
        name: "秒杀活动",
        beforeEnter(to, from, next) {
          fn("/index/seckill") ? next() : next("/index");
        }
      },
      {
        path: "/index",
        redirect: "/index/home"
      }
    ]
  },
  {
    path: "/login",
    component: Login,
    meta: { title: "登录" },
  },
  {
    path: "*",
    redirect: "/login"
  },
  //   {
  //     path: '/',
  //     name: 'Home',
  //     component: Home
  //   },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  routes
})


router.beforeEach((to, from, next) => {
  if (to.path !== "/login") {
    if (sessionStorage.getItem("user")) {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
})
export default router
